<?php TPL::output('global/header_meta.tpl.htm'); ?>
<style type="text/css"> 
.show {
    display: block;
}
.hide {
    display: none;
}
#notice {
    color: red;
}
#embed-captcha span {
    position: static;
}  
</style>
<div id="wrapper">
	<div class="aw-login-box">
		<div class="mod-body clearfix">
			<div class="content pull-left">
				<h1 class="logo"><a href=""></a></h1>
				<h2><?php echo get_setting('site_name'); ?></h2>
				<form id="login_form" method="post" onsubmit="return false" action="account/ajax/login_mobile_process/">
					<input type="hidden" name="return_url" value="<?php echo $this->return_url; ?>" />
					<ul>
						<li>
	                        <input class="aw-register-email form-control" id="phone" name="mobile" type="text" placeholder="手机号" tips="<?php _e('请输入你正在使用的手机号'); ?>" value="<?php echo htmlspecialchars($_GET['mobile']); ?>"  errortips="<?php _e('手机号不正确'); ?>" >
	                    </li>

						<li class="aw-register-verify">
                            <div id="embed-captcha"></div>
                            <p id="wait" class="show">正在加载验证码......</p>
                            <p id="notice" class="hide">请先完成验证</p>
                        </li>

						<li>
	                        <input class="aw-register-email form-control" style="width: 160px;margin-right: 20px;display: inline-block;" name="smscode" type="text"  placeholder="验证码" tips="<?php _e('请准确输入收到的短信验证码'); ?>" errortips="<?php _e('短信验证码不正确'); ?>" >

	                        <button id="Smsbtn" type="button"  class="pull-right btn btn-large btn-primary">获取验证码</button>
	                    </li>

						<li class="alert alert-danger collapse error_message">
							<i class="icon icon-delete"></i> <em></em>
						</li>
						<li class="last">
							<a href="javascript:;" onclick="AWS.ajax_post($('#login_form'), AWS.ajax_processer, 'error_message');" id="login_submit" class="pull-right btn btn-large btn-primary"><?php _e('登录'); ?></a>
							<label>
								<input type="checkbox" value="1" name="net_auto_login" />
								<?php _e('记住我'); ?>
							</label>
							<a href="account/find_password/">&nbsp;&nbsp;<?php _e('忘记密码'); ?></a>
						</li>
					</ul>
				</form>
			</div>
			<div class="side-bar pull-left">
				<?php if (get_setting('site_close') != 'Y' && (get_setting('sina_weibo_enabled') == 'Y' || get_setting('qq_login_enabled') == 'Y' || get_setting('google_login_enabled') == 'Y' || get_setting('facebook_login_enabled') == 'Y' || get_setting('twitter_login_enabled') == 'Y' || get_setting('weixin_app_id') && get_setting('weixin_app_secret') && get_setting('weixin_account_role') == 'service')) { ?>

					<?php if ($this->return_url) $return_url = 'return_url-' . base64_encode($this->return_url); ?>

					<h3><?php _e('第三方账号登录'); ?></h3>
					<?php if (get_setting('sina_weibo_enabled') == 'Y') { ?>
						<a href="account/openid/weibo/bind/<?php echo $return_url; ?>" class="btn btn-block btn-weibo"><i class="icon icon-weibo"></i> 微博登录</a>
					<?php } ?>

					<?php if (get_setting('qq_login_enabled') == 'Y') { ?>
						<a href="account/openid/qq/bind/<?php echo $return_url; ?>" class="btn btn-block btn-qq"><i class="icon icon-qq"></i> QQ 登录</a>
					<?php } ?>

					<?php if (get_setting('weixin_app_id') && get_setting('weixin_app_secret') && get_setting('weixin_account_role') == 'service') { ?>
						<a href="account/weixin_login/<?php echo $return_url; ?>" class="btn btn-block btn-wechat">
							<i class="icon icon-wechat"></i> 微信扫一扫登录
							<div class="img">
								<img src="<?php echo get_js_url('/weixin/login_qr_code/'); ?>" />
							</div>
						</a>
					<?php } ?>

					<?php if (get_setting('google_login_enabled') == 'Y') { ?>
						<a href="account/openid/google/bind/<?php echo $return_url; ?>" class="btn btn-block btn-google"> <i class="icon icon-google"></i> Google 登录</a>
					<?php } ?>

					<?php if (get_setting('facebook_login_enabled') == 'Y') { ?>
						<a href="account/openid/facebook/bind/<?php echo $return_url; ?>" class="btn btn-block btn-facebook"> <i class="icon icon-facebook"></i> Facebook 登录</a>
					<?php } ?>

					<?php if (get_setting('twitter_login_enabled') == 'Y') { ?>
						<a href="account/openid/twitter/bind/<?php echo $return_url; ?>" class="btn btn-block btn-twitter"> <i class="icon icon-twitter"></i> Twitter 登录</a>
					<?php } ?>
				<?php } ?>
			</div>
		</div>
		<div class="mod-footer">
			<span><?php _e('还没有账号?'); ?></span>&nbsp;&nbsp;
			<a href="account/register/"><?php _e('立即注册'); ?></a>&nbsp;&nbsp;•&nbsp;&nbsp;
			<a href="/"><?php _e('游客访问');?></a>&nbsp;&nbsp;•&nbsp;&nbsp;
			<a href="reader/"><?php _e('问答阅读'); ?></a>&nbsp;&nbsp;•&nbsp;&nbsp;
            <a href="account/login/"><?php _e('账号登录'); ?></a>
		</div>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function () { 
    $('#login_form input').keydown(function (e) {
        if (e.keyCode == 13)
        {
            $('#login_submit').click();
        }
    });

    var check_weixin_login;

    $('.btn-wechat').mouseover(function()
    {
    	if ($(this).find('img').length)
    	{
    		$(this).addClass('active');
    	}

    	check_weixin_login = setInterval(function ()
    	{
			$.get(G_BASE_URL + '/account/ajax/weixin_login_process/', function (response) {
				if (response.errno == 1)
				{
                    if ($('input[name="return_url"]').val())
                    {
                        window.location.href = $(input[name="return_url"]).val();
                    }
                    else
                    {
                        window.location.reload();
                    }
				}
			}, 'json');
		}, 1500);

    });

    $('.btn-wechat').mouseout(function()
    {
    	$(this).removeClass('active');

    	clearInterval(check_weixin_login);
    });

    verify_register_form('#login_form');


    /* 登陆页面验证 */
    function verify_register_form(element)
    {

        $(element).find('[type=text], [type=password]').on({
            focus : function()
            {
                if (typeof $(this).attr('tips') != 'undefined' && $(this).attr('tips') != '')
                {
                    $('.error_message em').html($(this).attr('tips'));
                }
            },
            blur : function()
            {
                if ($(this).attr('tips') != '')
                {
                    switch ($(this).attr('name'))
                    {
                        case 'mobile':
                            $(this).parent().find('.aw-reg-tips').detach();
                            var phonereg = /^1[345789]\d{9}$/;
                            if (!phonereg.test($(this).val()))
                            {   
                            	$('.error_message').fadeIn();

                            	$('.error_message em').html($(this).attr('errortips'));

                                return;
                            }
                            else
                            {   
                                $('.error_message').fadeOut();
                            }
                            return;

                        case 'smscode':
                            $(this).parent().find('.aw-reg-tips').detach();
                            if (!$(this).val())
                            {
                                $('.error_message').fadeIn();
                                $('.error_message em').html($(this).attr('errortips'));
                                return;
                            }
                            else
                            {
                                $('.error_message').fadeOut();
                            }
                            return;

                    }
                }

            }
        });
    }

    if($.cookie("captcha")){
        var count = $.cookie("captcha");
        var btn = $('#Smsbtn');
        $('#Smsbtn').prop('disabled', true);
        btn.text(count+'秒');
        var resend = setInterval(function(){
            count--;
            if (count > 0){
                // btn.text(count+'秒后可重新获取')
                btn.text(count+'秒');
                $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
            }else {
                $('#Smsbtn').prop('disabled', false);
                clearInterval(resend);
                btn.text("获取验证码")
            }
        }, 1000);
    }
    
    var handlerEmbed = function (captchaObj) {
        $("#Smsbtn").click(function (e) {
            var phonereg = /^1[3456789]\d{9}$/;
            var mobile = $('input[name="mobile"]').val();

            if(mobile=='' ){
                AWS.alert("请输入手机号");
                return false;
            }
            if(!phonereg.test(mobile)){
                AWS.alert("手机号格式不正确");
                return false;
            }
            if($('#Smsbtn').text()!="获取验证码"){
                return false;
            }

            var validate = captchaObj.getValidate();
            if (!validate) {
                AWS.alert("请先完成验证");
                return false;
            } else{
                $.post(G_BASE_URL + '/account/ajax/check_mobile/',{
                    mobile:mobile,
                    type:'find_password',
                }, function (result)
                {
                    if (result.errno == -1)
                    {
                        $('.error_message').fadeIn();
                        $('.error_message em').html(result.err);
                        captchaObj.reset();
                        return false;
                    }else{
                        $('.error_message').fadeOut();
                        $.ajax({
                            url: G_BASE_URL + '/tools/sendSms/',
                            type: "post",
                            dataType: "json",
                            data: {
                                mobile: mobile,
                                challenge: '',
                                geetest_challenge: validate.geetest_challenge,
                                geetest_validate: validate.geetest_validate,
                                geetest_seccode: validate.geetest_seccode,
                                client_type: 'web',
                                genre: '1'
                            },
                            success: function (result) {
                                if(result.errno == -1)
                                {
                                    AWS.alert(result.err);
                                    captchaObj.reset(); // 调用该接口进行重置 
                                    return false;
                                }else
                                {
                                    AWS.alert(result.err);
                                    var count = 60;
                                    var inl = setInterval(function () {
                                        $('#Smsbtn').prop('disabled', true);
                                        count -= 1;
                                        var text = count + ' 秒';
                                        $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
                                        $('#Smsbtn').text(text);
                                        if (count <= 0) {
                                            clearInterval(inl);
                                            $('#Smsbtn').prop('disabled', false);
                                            $('#Smsbtn').text('获取验证码');
                                        }
                                    }, 1000);
                                    return true;
                                }

                            }
                        });
                    }
                }, 'json');
            }
        });
        // 将验证码加到id为captcha的元素里，同时会有三个input的值：geetest_challenge, geetest_validate, geetest_seccode
        captchaObj.appendTo("#embed-captcha");
        captchaObj.onReady(function () {
            $("#wait")[0].className = "hide";
        });
    };

    $.ajax({
        // 获取id，challenge，success（是否启用failback）
        url: G_BASE_URL+ "/account/slide_captcha/?t=" + (new Date()).getTime()+"&client_type='web'", // 加随机数防止缓存
        type: "get",
        dataType: "json",
        success: function (data) {
            var dataObj = eval('(' + data.rsm + ')');
            // 使用initGeetest接口
            // 参数1：配置参数
            // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它做appendTo之类的事件
            initGeetest({
                gt: dataObj.gt,
                challenge: dataObj.challenge,
                new_captcha: dataObj.new_captcha,
                offline: !dataObj.success, // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                product: "embed", // 产品形式，包括：float，embed，popup。注意只对PC版验证码有效
                width: '100%' // 设置按钮宽度, 与父元素宽度一致
            }, handlerEmbed);
        }
    });
});
</script>
<!-- <script type="text/javascript" src="<?php echo G_STATIC_URL; ?>/js/app/login.js"></script> -->

<?php TPL::output('global/footer.tpl.htm'); ?>